package com.funong.matrix.biz.system.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.funong.matrix.biz.base.dao.SysTagDao;
import com.funong.matrix.biz.base.entity.SysTagEntity;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.utils.DateTimeUtil;
import com.funong.matrix.core.utils.JwtUtils;
import com.funong.matrix.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:56
 **/
@Slf4j
@Service
public class SysTagDomainDao {
    @Resource
    private SysTagDao tagDao;

    public Boolean saveTag(SysTagEntity sysTagEntity){
        String currentUserName = JwtUtils.getCurrentUserName();
        if(StringUtils.isNotBlank(currentUserName)){
            sysTagEntity.setCreatedBy(currentUserName);
        }
        sysTagEntity.setCreatedAt(DateTimeUtil.now())
                .setUpdatedAt(DateTimeUtil.now());
        return tagDao.save(sysTagEntity);
    }

    public Boolean deleteTag(Integer id){
        return tagDao.removeById(id);
    }

    public Boolean updateTag(SysTagEntity sysTagEntity){
        String currentUserName = JwtUtils.getCurrentUserName();
        if(StringUtils.isNotBlank(currentUserName)){
            sysTagEntity.setUpdatedBy(currentUserName);
        }
        sysTagEntity.setUpdatedAt(DateTimeUtil.now());
        return tagDao.updateById(sysTagEntity);
    }

    public Result queryTag(QueryDTO<SysTagEntity> queryDTO) {
        IPage<SysTagEntity> sysTagEntityIPage = tagDao.queryTag(queryDTO);
        return Result.success(sysTagEntityIPage);
    }
}
